package com.zmn.oms.admin.controller.work.modify;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.constant.*;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.dto.work.modify.OrderMultipleVisitDTO;
import com.zmn.oms.model.dto.work.modify.OrderWorkSuspendDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.visit.CancelMultipleVisitVO;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkMultipleVisitBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;

/**
 * 类描述： 多次上门
 *
 * @author yule
 * @date 2021/3/11 15:54
 */
@Slf4j
@Controller
@RequestMapping("/work/modify")
public class WorkMultipleVisitController extends WorkBaseController {

    @Autowired
    ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    private OrderWorkMultipleVisitBService orderWorkMultipleVisitBService;


    /**
     * 多次上门
     */
    @RequestMapping(value = "/multiple/visit")
    public ModelAndView multipleVisit(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/multiplevisit");

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        List<KVDict> kvDictList = Lists.newArrayListWithCapacity(2);
        if (Objects.equals(orderWork.getStatus(), OrderStatusConsts.WORK_STATUS_VISIT)) {
            if (Objects.equals(orderWork.getServCategId(), ProductConsts.SERV_CATEG_WATERPROOF)
                    || Objects.equals(orderWork.getServCategId(), ProductConsts.SERV_CATEG_METOPE)
                    || Objects.equals(orderWork.getServCategId(), ProductConsts.SERV_CATEG_KITCHEN_TOILET)) {
                mv.addObject("orderWork", orderWork);
                kvDictList.add(KVDict.builder()
                        .value(OrderVisitStatusConsts.ORDER_VISIT_TYPE_OTHER)
                        .text(OrderVisitStatusConsts.getVisitTypeName(OrderVisitStatusConsts.ORDER_VISIT_TYPE_OTHER))
                        .build());
            } else {
                kvDictList.add(KVDict.builder()
                        .value(OrderVisitStatusConsts.ORDER_VISIT_TYPE_DELIVERY)
                        .text(OrderVisitStatusConsts.getVisitTypeName(OrderVisitStatusConsts.ORDER_VISIT_TYPE_DELIVERY))
                        .build());
                kvDictList.add(KVDict.builder()
                        .value(OrderVisitStatusConsts.ORDER_VISIT_TYPE_WAIT_PART)
                        .text(OrderVisitStatusConsts.getVisitTypeName(OrderVisitStatusConsts.ORDER_VISIT_TYPE_WAIT_PART))
                        .build());
            }
        }
        // 上门缘由
        mv.addObject("visitTypeList", kvDictList);
        mv.addObject("orderWork", orderWork);
        return mv;
    }

    /**
     * 保存多次上门
     */
    @RequestMapping(value = "/save/multiple/visit")
    @ResponseBody
    public ResultDTO saveMultipleVisit(@RequestBody OrderMultipleVisitDTO orderMultipleVisitDTO) {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderMultipleVisitDTO.getOrderId(), orderMultipleVisitDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_MULTIPLE_VISIT);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        try {
            orderMultipleVisitDTO.setOperateTime(DateUtil.getNow());
            orderWorkMultipleVisitBService.addOrderMultipleVisit(orderMultipleVisitDTO);
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }

        return ResultDTO.success();
    }

    /**
     * 取消多次上门
     */
    @RequestMapping(value = "/cancel/multiple/visit")
    public ModelAndView cancelMultipleVisit(Long orderId, Long workId, Long visitId) {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        ModelAndView mv = new ModelAndView("work/modify/cancelMultipleVisit");
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        mv.addObject("masterId", orderWork.getMasterId());
        mv.addObject("visitId", visitId);
        CancelMultipleVisitVO vo = orderWorkMultipleVisitBService.getCancelMultipleVisitVO(orderWork);
        mv.addObject("cancelList", vo.getCancelList());
        return mv;
    }


    /**
     * 取消多次上门
     */
    @RequestMapping(value = "/save/cancel/multiple/visit")
    @ResponseBody
    public ResultDTO cancelMultipleVisit(@RequestBody OrderMultipleVisitDTO orderMultipleVisitDTO) {

        try {
            orderMultipleVisitDTO.setOperateTime(DateUtil.getNow());
            orderWorkMultipleVisitBService.cancelAgainVisit(orderMultipleVisitDTO);
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }

        return ResultDTO.success();
    }
}
